/*
 * 2006-1-11 lxm 创建。 
 */
package com.hd123.rumba.sql;

import com.hd123.rumba.exception.BusinessException;

/**
 * 查询语句（定义） | 数据交换类
 * <p>
 * 
 * 对应SQLSubquery。使用Hibernate查询语言（hql）。
 * 
 * @author lxm
 * 
 */
public class DSQLSubquery {

  /** 是否去重 */
  private Boolean isDistinct = Boolean.FALSE;
  /** Select 子句 */
  private DSQLSubqueryParam[] selectClause;
  /** From 子句 */
  private DSQLSubqueryParam[] fromClause;
  /** Where 子句 */
  private DSQLSubqueryParam[] whereClause;
  /** Group By 子句 */
  private DSQLSubqueryParam[] groupClause;
  /** Having 子句 */
  private DSQLSubqueryParam[] havingClause;
  /** Order By 子句 */
  private DSQLSubqueryParam[] orderClause;

  /**
   * 取得对应的SQLSubquery对象
   * 
   * @return 一个新建的SQLSubquery对象。
   */
  public SQLSubquery createSQLSubquery() throws BusinessException {
    SQLSubquery sq = new SQLSubquery();
    assignSelectClause(sq);
    assignFromClause(sq);
    assignWhereClause(sq);
    assignGroupClause(sq);
    assignHavingClause(sq);
    assignOrderClause(sq);
    return sq;
  }

  private void assignSelectClause(SQLSubquery sq) throws BusinessException {
    for (int i = 0; i < selectClause.length; i++) {
      if (i == 0 && Boolean.TRUE.equals(isDistinct))
        sq.addSelect("distinct " + selectClause[i].getParams()[0]);
      else
        sq.addSelect(selectClause[i].getParams()[0]);
    }
  }

  private void assignFromClause(SQLSubquery sq) {
    for (int i = 0; i < fromClause.length; i++) {
      if (fromClause[i].getParams().length == 1)
        sq.addFrom(fromClause[i].getParams()[0]);
      else if (fromClause[i].getParams().length >= 2)
        sq.addFrom(fromClause[i].getParams()[0], fromClause[i].getParams()[1]);
    }
  }

  private void assignWhereClause(SQLSubquery sq) {
    for (int i = 0; i < whereClause.length; i++) {
      if (whereClause[i].getParams().length >= 1)
        sq.addWhere(whereClause[i].getParams()[0]);
    }
  }

  private void assignGroupClause(SQLSubquery sq) {
    for (int i = 0; i < groupClause.length; i++) {
      if (groupClause[i].getParams().length >= 1)
        sq.addGroup(groupClause[i].getParams()[0]);
    }
  }

  private void assignHavingClause(SQLSubquery sq) {
    for (int i = 0; i < havingClause.length; i++) {
      if (havingClause[i].getParams().length >= 1)
        sq.addHaving(havingClause[i].getParams()[0]);
    }
  }

  private void assignOrderClause(SQLSubquery sq) {
    for (int i = 0; i < orderClause.length; i++) {
      if (orderClause[i].getParams().length == 1)
        sq.addOrder(orderClause[i].getParams()[0]);
      else if (orderClause[i].getParams().length >= 2)
        sq.addOrder(orderClause[i].getParams()[0], orderClause[i].getParams()[1]);
    }
  }

  public DSQLSubqueryParam[] getFromClause() {
    return fromClause;
  }

  public void setFromClause(DSQLSubqueryParam[] fromClause) {
    this.fromClause = fromClause;
  }

  public DSQLSubqueryParam[] getGroupClause() {
    return groupClause;
  }

  public void setGroupClause(DSQLSubqueryParam[] groupClause) {
    this.groupClause = groupClause;
  }

  public DSQLSubqueryParam[] getHavingClause() {
    return havingClause;
  }

  public void setHavingClause(DSQLSubqueryParam[] havingClause) {
    this.havingClause = havingClause;
  }

  public DSQLSubqueryParam[] getOrderClause() {
    return orderClause;
  }

  public void setOrderClause(DSQLSubqueryParam[] orderClause) {
    this.orderClause = orderClause;
  }

  public DSQLSubqueryParam[] getSelectClause() {
    return selectClause;
  }

  public void setSelectClause(DSQLSubqueryParam[] selectClause) {
    this.selectClause = selectClause;
  }

  public DSQLSubqueryParam[] getWhereClause() {
    return whereClause;
  }

  public void setWhereClause(DSQLSubqueryParam[] whereClause) {
    this.whereClause = whereClause;
  }

  public Boolean getIsDistinct() {
    return isDistinct;
  }

  public void setIsDistinct(Boolean isDistinct) {
    this.isDistinct = isDistinct;
  }

}
